package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.service.impl.BiDZColorServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiDZColorServlet extends AbstractBaseServlet {
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 10;
	public final static int ACTION_GET_SUGGESTION_LIST = 20;

	private static final long serialVersionUID = -4514538281621552309L;
		
	private BiDZColor biDZColor;

	private BiDZColorServiceImpl dzColorService;
	
	public BiDZColorServlet() {
		super();
		this.simpleName = BiDZColor.class.getSimpleName();
		this.fullName = BiDZColor.class.getName();
		this.dzColorService = new BiDZColorServiceImpl();
		this.dzColorService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.dzColorService.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		super.setAcUser(request, this.dzColorService);
		String rtn = "";
		
		String ctYarnMaterial = request.getParameter("ctYarnMaterial");
		String ctColorFamily = request.getParameter("ctColorFamily");
		String ctFilsType = request.getParameter("ctFilsType");
		String ctSeasonNumber = request.getParameter("ctSeasonNumber");
		String yarnId = request.getParameter("yarnId");
		String customerId = request.getParameter("customerId");
		switch (this.action) {
			case ACTION_GET: // "get one"
				super.setId(request);
				rtn = this.dzColorService.get(this.id);
				break;
			case ACTION_GET_WITH_DESCRIPTION:
				super.setId(request);
				rtn = this.dzColorService.getWithDescription(this.id);
				break;
			case ACTION_DELETE:
				super.setId(request);
				rtn = this.dzColorService.delete(this.id);
				break;
			case ACTION_GET_ALL_PAGINATION_FILTER:
				super.setPaginationParam(request);
				super.setFilterParam(request);
				rtn = this.dzColorService.getAllPagination(this.page, this.sort,
						this.dir, super.filters);
				break;
			case ACTION_EDIT:
				this.fillObj(request);
				rtn = this.dzColorService.edit(this.biDZColor);
				break;
			case ACTION_GET_MAX_REFERENCE_INDEX:
				
				rtn = this.dzColorService.getMaxReferenceIndex(ctYarnMaterial, ctColorFamily, ctFilsType);
				break;
			case ACTION_GET_SUGGESTION_LIST:
				super.setPaginationParam(request);
				super.setFilterParam(request);
				
				String[] tempFilter = null;
				if(super.filters == null){
					super.filters = new String[0];
				}
				if(ctYarnMaterial != null){
					tempFilter = new String[this.filters.length + 3];
					System.arraycopy(this.filters, 0, tempFilter, 0, this.filters.length);
					tempFilter[this.filters.length] = "ctYarnMaterial = '" + ctYarnMaterial  +"'";
					tempFilter[this.filters.length + 1] = "ctColorFamily = '" + ctColorFamily  +"'";
					tempFilter[this.filters.length + 2] = "ctFilsType = '" + ctFilsType  +"'";
				}else if(ctSeasonNumber != null){
					tempFilter = new String[this.filters.length + 3];
					System.arraycopy(this.filters, 0, tempFilter, 0, this.filters.length);
					tempFilter[this.filters.length] = "ctSeasonNumber = '" + ctSeasonNumber  +"'";
					tempFilter[this.filters.length + 1] = "biYarn.id=" + yarnId;
					tempFilter[this.filters.length + 2] = "biFranceCustomer.id=" + customerId;
				}
				super.filters = tempFilter;
				rtn = this.dzColorService.getAllPagination(this.page, this.sort,
						this.dir, super.filters);
				break;
		}
		return rtn;
	}
	
	private void fillObj(HttpServletRequest request) throws Exception{
		super.fillObj(request);
		this.biDZColor = (BiDZColor)this.obj;
		
		String yarnId = (String)this.getValue(request, "yarnId");
		if(!StringUtils.isEmpty(yarnId)){
			BiYarn biYarn = new BiYarn();
			biYarn.setId(Integer.parseInt(yarnId));
			this.biDZColor.setBiYarn(biYarn);
		}
		
		String customerId = (String)this.getValue(request, "customerId");
		if(!StringUtils.isEmpty(customerId)){
			BiFranceCustomer customer = new BiFranceCustomer();
			customer.setId(Integer.parseInt(customerId));
			this.biDZColor.setBiFranceCustomer(customer);
		}
		
		String factoryId = (String)this.getValue(request, "factoryId");
		if(!StringUtils.isEmpty(factoryId)){
			BiFactory factory = new BiFactory();
			factory.setId(Integer.parseInt(factoryId));
			this.biDZColor.setBiFactory(factory);
		}
		
		String oldId = (String)this.getValue(request, "oldId");
		if(!StringUtils.isEmpty(oldId)){
			BiDZColor old = new BiDZColor();
			old.setId(Integer.parseInt(oldId));
			this.biDZColor.setBiDZColor(old);
		}
	}
	
}
